<?php

  //Разработал Храновский И.П. (http://web.uix.cc)
  //web.johns.reviews@gmail.com
  //UNGINE http://code.google.com/p/ungine-php/

ob_start();

//Collecting Some Debug Information
define('debug', false);
if(debug == true){
    error_reporting(E_ALL);
    $mtime = microtime();
    $mtime = explode(" ",$mtime);
    $mtime = $mtime[1] + $mtime[0];
    $starttime = $mtime;
    $before = memory_get_usage();
}

//Enable Core Features (Low Level)
require_once('system/config.php');
require_once('system/route.map.php');
require_once('system/autoloader.php');

//Make Asosiation (Aliasing)
$G =& $_GET;
$P =& $_POST;

//Instantiation Of Main (Core) Class (Controller/View Level)
$application = new Application();
if($db_user!=""){
    $application->db = new ezSQL(db_user, db_password, db_name, db_host);
}
$application->session = new SecureSession(session_key);

//Enabling Localization Support (Top Level)
if(strlen($application->session->get('selected_language')) == 0){
    if(strlen(get_cookie('selected_language')) == 0){
        $application->session->set('selected_language','default');
    }else{
        $application->session->set('selected_language',get_cookie('selected_language'));
    }
}

//Breaking URL
//http://uix.cc/engine.data into
//$G['controller'] => 'engine';
//$G['get_id']     => 'data';
$parts = explode('.', $G['controller']);
$G['controller'] = $parts[0];

//Fixing http://uix.cc/data/ => http://uix.cc/data
if(substr($G['controller'], -1, 1)=="/"){
    $G['controller'] = substr($G['controller'],0,-1);
}

//Breaking URL Fix
$ii=0;
$set=null;
foreach ($parts as $item){
    if($ii>0){
        $set .= ".".$item;
    }
    $ii++;
}
$O['get_id'] = substr($set,1);

//Loading Localization Package (Application Level)
$application->package('general');

//Finaly Load Our Controller
if(strlen($G['controller'])>0){
    $application->controller($G['controller'],$data);
}else{
    $application->controller('main',$data);
}

//Displaying Debug Information
if(debug==true){
echo "<div align='left' style='border: 2px solid #C0FCA6; background-color: #DEFFD8; padding: 8px; margin: 4px;'>";

   $mtime = microtime();
   $mtime = explode(" ",$mtime);
   $mtime = $mtime[1] + $mtime[0];
   $endtime = $mtime;
   $totaltime = ($endtime - $starttime);

echo "max memory allocated: ".round(memory_get_peak_usage()/1024)." kb<br/>";
echo "compiled in ".substr($totaltime,0,6)." s<hr/>";

$print = get_required_files();

echo "<table style='border: 1px solid red;' width='100%'><tr><td style='border: 1px solid red;'>";
foreach($print as $key){
	echo " ".substr(strstr($key,"www"),3)."<br/>";
}
echo "</td><td style='border: 1px solid red;'>";
$print = get_defined_constants(true);
foreach($print['user'] as $key=>$value){
	echo "const <i>$key</i> => $value<br/>";
}

echo "</td><td style='border: 1px solid red;'>";
foreach($_COOKIE as $key=>$value){
	echo "#COOKIE[<i>$key</i>] => $value<br/>";
}
foreach($_GET as $key=>$value){
	echo "#G[<i>$key</i>] => $value<br/>";
}
foreach($_POST as $key=>$value){
	echo "#P[<i>$key</i>] => $value<br/>";
}
foreach($_SESSION as $key=>$value){
	echo "<font color='gray'>#S[<i>$key</i>] => $value</font><br/>";
        echo "#S[<i>$key</i>] => ".$S->get($key)."<br/>";
}
echo "</td></tr></table>";

foreach($_SERVER as $k=>$v){
    	echo "<font color='blue'>#S[<i>$k</i>] -> $v</font><br/>";
}

echo $O['db']->debug();
echo "</div>";
}


?>
